Imports System.Runtime.Serialization
Imports System.Runtime.Versioning

Namespace DataLogging

    ''' <summary>
    ''' Consult Log Database. A database of consult logs
    ''' </summary>
    ''' <remarks></remarks>
    <Serializable(), ObfuscationAttribute(Exclude:=True, ApplyToMembers:=True)> Friend Class ConsultLogDB
        Inherits ArrayList
        Implements IDisposable

        'Private m_arrayDB As New ArrayList(100)

        Public Shadows Function Add(ByVal value As ConsultLog) As Integer
            Dim f_match As Boolean = False
            '' --------------- If more than 1 in array, see if match.. if match then replace
            If Me.Count >= 1 Then
                For i As Integer = 0 To Me.Count - 1
                    If CType(Me(i), ConsultLog).Header = value.Header Then
                        f_match = True
                        Me(i) = value
                        Exit For
                    End If
                Next
            End If
            If Not f_match Then
                Return MyBase.Add(value)
            End If
        End Function
        Public Shadows Function ToArray() As ConsultLog()
            Return CType(MyBase.ToArray(GetType(ConsultLog)), ConsultLog())
        End Function
        Default Public Shadows Property Item(ByVal index As Integer) As ConsultLog
            Get
                Return CType(MyBase.Item(index), DataLogging.ConsultLog)
            End Get
            Set(ByVal value As ConsultLog)
                MyBase.Item(index) = value
            End Set
        End Property

        Private disposedValue As Boolean = False        ' To detect redundant calls

        ' IDisposable
        Protected Overridable Sub Dispose(ByVal disposing As Boolean)
            If Not Me.disposedValue Then
                If disposing Then
                    ' TODO: free managed resources when explicitly called
                End If
                Me.Clear()

                ' TODO: free shared unmanaged resources
            End If
            Me.disposedValue = True
        End Sub

#Region " IDisposable Support "
        ' This code added by Visual Basic to correctly implement the disposable pattern.
        Public Sub Dispose() Implements IDisposable.Dispose
            ' Do not change this code.  Put cleanup code in Dispose(ByVal disposing As Boolean) above.
            Dispose(True)
            GC.SuppressFinalize(Me)
        End Sub
#End Region

    End Class

    <Serializable(), ObfuscationAttribute(Exclude:=True, ApplyToMembers:=True)> Friend Class ConsultLog
        Inherits ArrayList
        Implements IDisposable

        'Private m_arrayLog As New ArrayList(65536)
        Private m_cursor As Integer = 0
        Private m_firstTimeStamp As DateTime = Now

        <OnDeserialized()> _
        Private Sub refresh(ByVal context As StreamingContext)
            ReIndex()
        End Sub

        Sub New()
            MyBase.New()
            'Me.Capacity = 256000
        End Sub

        Public Shadows Sub AddRange(ByVal c As Consult_Args())
            If Not IsNothing(c) Then
                For i As Integer = 0 To c.Length - 1
                    Me.Add(c(i))
                Next
            End If
            'MyBase.AddRange(c)
        End Sub
        Public Shadows Function Add(ByVal value As Consult_Args) As Integer
            If Not IsNothing(value) Then
                If Me.Count >= 256000 Then
                    Me.RemoveRange(0, 16386)
                    ReIndex()
                ElseIf Me.Count = 0 Then
                    m_firstTimeStamp = Now
                End If
                value.Index = Me.Count + 1

                Return MyBase.Add(value)
            End If
        End Function

        Default Public Shadows Property Item(ByVal index As Integer) As Consult_Args
            Get
                If index >= 0 And index < Me.Count Then
                    'Try
                    Dim f_return As Consult_Args = CType(MyBase.Item(index), Consult_Args)
                    'If f_return.Index <> index + 1 Then
                    '    ReIndex()
                    '    f_return = CType(MyBase.Item(index), Consult_Args)
                    'End If
                    Return f_return
                    'Catch
                    'Return Nothing
                    'End Try
                Else
                    Return Nothing
                End If
            End Get
            Set(ByVal value As Consult_Args)
                MyBase.Item(index) = value
            End Set
        End Property
        Public Shadows ReadOnly Property GetRange(ByVal index As Integer, ByVal length As Integer) As ConsultLog
            Get
                Dim f_log As New ConsultLog
                For i As Integer = index To index + length
                    f_log.Add(Me(i))
                Next
                Return f_log
            End Get
        End Property

        Friend Sub ReIndex()
            Dim c As Integer = Me.Count
            For i As Long = 1 To c
                If Not IsNothing(Me(i - 1)) Then Me(i - 1).Index = i
            Next
        End Sub
        Public Overrides Function ToString() As String
            Return Me.HeaderTxt
        End Function
        Friend ReadOnly Property Header() As DateTime
            Get
                Return m_firstTimeStamp
            End Get
        End Property
        Friend ReadOnly Property HeaderTxt() As String
            Get
                With m_firstTimeStamp
                    Return .ToString("dd/MM/yyyy hh:mm:ss")
                    'Dim f_str As String = .Day & "/" & .Month & "/" & .Year & " " & .Hour & ":" & .Minute
                    'Return f_str
                End With
            End Get
        End Property

        Friend ReadOnly Property Last() As Consult_Args
            Get
                If Me.Count >= 1 Then Return Me(Me.Count - 1) Else Return Nothing
            End Get
        End Property
        Friend ReadOnly Property readNext() As Consult_Args
            Get
                If Not IsNothing(Me(m_cursor + 1)) Then
                    m_cursor += 1
                    Return Me(m_cursor)
                Else
                    Return Nothing
                End If
            End Get
        End Property

        Friend ReadOnly Property readPrevious() As Consult_Args
            Get
                If m_cursor >= 1 Then
                    If Not IsNothing(Me(m_cursor - 1)) Then
                        m_cursor -= 1
                        Return Me(m_cursor)
                    Else
                        Return Nothing
                    End If
                Else
                    Return Me(m_cursor)
                End If
            End Get
        End Property

        Public Shadows Function ToArray() As Consult_Args()
            If Me.Count >= 1 Then
                Dim arrConsultLog(Me.Count - 1) As Consult_Args
                For i As Integer = 0 To Me.Count - 1
                    arrConsultLog(i) = Me(i)
                Next

                'Return CType(MyBase.ToArray(), Consult_Args())
                Return arrConsultLog
            Else
                Return Nothing
            End If
        End Function

        Private disposedValue As Boolean = False        ' To detect redundant calls

        ' IDisposable
        Protected Overridable Sub Dispose(ByVal disposing As Boolean)
            If Not Me.disposedValue Then
                If disposing Then
                    ' TODO: free managed resources when explicitly called
                End If
                Me.Clear()
                ' TODO: free shared unmanaged resources
            End If
            Me.disposedValue = True
        End Sub

#Region " IDisposable Support "
        ' This code added by Visual Basic to correctly implement the disposable pattern.
        Public Sub Dispose() Implements IDisposable.Dispose
            ' Do not change this code.  Put cleanup code in Dispose(ByVal disposing As Boolean) above.
            Dispose(True)
            GC.SuppressFinalize(Me)
        End Sub
#End Region

    End Class
End Namespace